package com.lpf.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 功能描述：写数据到文件
 *
 * @author lpf
 * @date 2019/11/24 21:42
 * @version: 1.0
 **/
public class NioTest03 {

    public static void main(String[] arg) throws Exception {

        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\LIUPEIFENG\\IdeaProjects\\Netty-Learning\\netty-zhanglong\\src\\main\\java\\com\\lpf\\nio\\text\\NioTest03.txt");
        FileChannel fileChannel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);

        byte[] message = "hello world welcome, nihao".getBytes();

        for (int i = 0; i < message.length; i++) {
            byteBuffer.put(message[i]);
        }

        byteBuffer.flip();

        fileChannel.write(byteBuffer);

        fileOutputStream.close();
    }

}
